

module alarm_ctrl(
	input			clk,
	input			rst_n,
    input           alarm_en,
	input           pulse_1us,
    input           VOLT_ALARM,
	input			TEMP_ALARM,
	input			PD_ALARM,	

	output  reg     pd_alarm,	
	output  reg     volt_alarm,	
	output  reg     temp_alarm
			
);		

localparam  [7:0]   PD_TIME     = 8'd120;
localparam  [7:0]   VOLT_TIME   = 8'd100;
localparam  [7:0]   TEMP_TIME   = 8'd100;

reg [ 7:0]	pd_cnt;
reg [ 7:0]	volt_cnt;
reg [ 7:0]	temp_cnt;
	
/****************pd alarm******************/
always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		pd_cnt <= 8'd0;
	else if(PD_ALARM == 1'b0 && alarm_en)begin
		if(pulse_1us && pd_cnt < PD_TIME)
			pd_cnt <= pd_cnt + 1'b1;
		else
			pd_cnt <= pd_cnt;
	end
    else
		pd_cnt <= 8'd0;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		pd_alarm <= 1'b0;
	else if(pd_cnt == PD_TIME)
		pd_alarm <= 1'b1;
    else
        pd_alarm <= pd_alarm;
end

/****************voltage alarm******************/
always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		volt_cnt <= 8'd0;
	else if(VOLT_ALARM == 1'b1)begin
		if(pulse_1us && volt_cnt < VOLT_TIME)
			volt_cnt <= volt_cnt + 1'b1;
		else
			volt_cnt <= volt_cnt;
	end
    else
        volt_cnt <= 8'd0;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		volt_alarm <= 1'b0;
	else if(volt_cnt == VOLT_TIME)
		volt_alarm <= 1'b1;
    else
        volt_alarm <= volt_alarm;
end

/****************temperature alarm******************/
always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		temp_cnt <= 8'd0;
	else if(TEMP_ALARM == 1'b1)begin
		if(pulse_1us && temp_cnt < TEMP_TIME)
			temp_cnt <= temp_cnt + 1'b1;
		else
			temp_cnt <= temp_cnt;
	end
    else
        temp_cnt <= 8'd0;
end

always @(posedge clk or negedge rst_n)begin
	if(!rst_n)
		temp_alarm <= 1'b0;
	else if(temp_cnt == TEMP_TIME)
		temp_alarm <= 1'b1;
    else
        temp_alarm <= temp_alarm;
end

endmodule